package com.cn.mmd.realm;


import com.cn.mmd.model.*;
import com.cn.mmd.service.*;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;



/**
 *
 * Created by Administrator on 2016/8/9 0009.
 */
public class SecurityRealm extends AuthorizingRealm {

 /*   @Autowired
    private SysAdminUserService sysAdminUserService;
*/
    /**
     * 权限验证
     * @param principalCollection s
     * @return s
     */
    @Override
    protected synchronized AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//       String loginName = (String) principalCollection.fromRealm(getName()).iterator().next();
//        System.out.println(loginName + "权限验证");
//        String [] s = loginName.split(",");
//        if (s.length>=3) {
//            int i1 = Integer.parseInt(s[1]);
//            ShiroUserRole shiroUserRole1 = new ShiroUserRole();
//            shiroUserRole1.setSchoolId(i1);
//            shiroUserRole1.setUserId(s[0]);
//            List<ShiroUserRole> shiroUserRoleList = shiroUserRoleService.selectByUserId(shiroUserRole1);
//            if (shiroUserRoleList != null && shiroUserRoleList.size() > 0) {
//                SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//                ShiroUserRole shiroUserRole;
//                ShrioRole shrioRole;
//                List<ShiroPermission> shiroPermissionList;
//                for (int i = 0; i < shiroUserRoleList.size(); i++) {
//                    shiroUserRole = shiroUserRoleList.get(i);
//                    shrioRole = shiroRoleService.selectByPrimaryKey(shiroUserRole.getRoleId());
//                    shiroPermissionList = shiroPermissionService.selectByRoleId(shiroUserRole.getRoleId());
//                    if (shrioRole != null) {
//                        simpleAuthorizationInfo.addRole(shrioRole.getRoleName());
//                    }
//                    if (shiroPermissionList != null && shiroPermissionList.size() > 0) {
//                        for (int j = 0; j < shiroPermissionList.size(); j++) {
//                            System.out.println(shiroPermissionList.get(j).getPermissionName());
//                            simpleAuthorizationInfo.addStringPermission(shiroPermissionList.get(j).getPermissionName());
//                        }
//                    }
//                }
//                return simpleAuthorizationInfo;
//            }
//        }
        return null;
    }

    /**
     * 登录验证
     * @param authenticationToken s
     * @return s
     * @throws org.apache.shiro.authc.AuthenticationException
     */
    @Override
    protected synchronized AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token=(UsernamePasswordToken) authenticationToken;
        String [] s = token.getUsername().split(",");
      /*  if (s.length>=3) {
            System.out.println(s[2]+","+s[0]);
            if (s[2].equals("1")) {
                SysAdminUser sysAdminUser = new SysAdminUser();
                sysAdminUser.setName(s[0]);
                sysAdminUser.setState(1);
                sysAdminUser = sysAdminUserService.selectByName(sysAdminUser);
                if (sysAdminUser != null) {
                    String password = sysAdminUser.getPassword();
                    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(token.getUsername(), password, getName());
                    return authenticationInfo;
                }
            }
        }*/
        return null;
    }

}
